package com.shunbo.convenient.controller;

import com.shunbo.convenient.service.GroupsService;
import com.shunbo.convenient.service.ManagerService;
import com.shunbo.convenient.utils.ConvenientResult;
import com.shunbo.convenient.vo.Groups;
import com.shunbo.convenient.vo.Manager;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.security.acl.Group;
import java.util.List;

/**
 * @author weizhuang
 * 地址
 */
@RestController
@RequestMapping("/managerInfo")
public class ManagerInfoController {


    @Resource
    private ManagerService managerService;
    @Resource
    private GroupsService groupsService;


    /**
     * 后管用户列表
     *
     * @param manager
     * @return Result结果集
     */
    @RequestMapping("/list")
    public ConvenientResult getManagerList(@RequestBody Manager manager) {
        try {
            List<Manager> all = managerService.findAll(manager);
            for (Manager man: all
                 ) {
                Groups byId = groupsService.findById(man.getGroupId());
                man.setGroups(byId);
            }
            return ConvenientResult.build(200, "成功", all);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * 后管用户列表
     *
     * @param manager
     * @return Result结果集
     */
    @RequestMapping("/login")
    public ConvenientResult getManagerLogin(@RequestBody Manager manager) {
        try {
            List<Manager> all = managerService.findAll(manager);
            if(all.size()==1){
                return ConvenientResult.build(200, "成功", all.get(0));
            }
            return ConvenientResult.build(400, "账户不存在或者重复", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }
    /**
     * 添加后管用户
     *
     * @param manager 图标
     * @return Result结果集
     */
    @RequestMapping("/save")
    public ConvenientResult saveManager(@RequestBody Manager manager) {
        try {
            Integer save = managerService.save(manager);
            return ConvenientResult.build(200, "成功", save);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * 更新后管用户
     *
     * @param manager 图标
     * @return Result结果集
     */
    @RequestMapping("/update")
    public ConvenientResult updateManager(@RequestBody Manager manager) {
        try {
            Integer save = managerService.update(manager);
            return ConvenientResult.build(200, "成功", save);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * 删除后管用户
     *
     * @param manager 图标
     * @return Result结果集
     */
    @RequestMapping("/delete")
    public ConvenientResult deleteManager(@RequestBody Manager manager) {
        try {
            Integer save = managerService.delete(manager.getId() + "");
            return ConvenientResult.build(200, "成功", save);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * id查后管用户
     *
     * @param manager 图标
     * @return Result结果集
     */
    @RequestMapping("/findOne")
    public ConvenientResult getManagerById(@RequestBody Manager manager) {
        try {
            Manager st = managerService.findById(manager.getId() + "");
            return ConvenientResult.build(200, "成功", st);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }



}
